import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/message")
public class MessageWall extends HttpServlet {
    //jackson
    ObjectMapper objectMapper = new ObjectMapper();

    //浏览器获取请求数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //显示传递响应格式，不要让浏览器去猜
        resp.setContentType("application/json; charset=utf8");
        //从数据库提取数据
        List<Message> messageList = null;
        try {
            messageList = load();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //返回给浏览器
        resp.getWriter().write(objectMapper.writeValueAsString(messageList));
    }

    //浏览器提交数据
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取json格式数据，并转换成java对象
        Message message = objectMapper.readValue(req.getInputStream(), Message.class);
        //将数据保存到数据库
        try {
            save(message);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //成功后打印日志
        resp.setStatus(200);
        System.out.println("数据提交成功, from: " + message.getFrom()
                + " to: " + message.getTo() + " message: " + message.getMessage());
    }

    //数据库操作
    //从数据库中取数据
    private List<Message> load() throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造sql
        String sql = "select * from message";
        PreparedStatement statement = connection.prepareStatement(sql);
        //执行sql
        ResultSet resultSet = statement.executeQuery();
        List<Message> messageList = new ArrayList<>();
        while(resultSet.next()) {
            Message message = new Message();
            message.setFrom(resultSet.getString("from"));
            message.setTo(resultSet.getString("to"));
            message.setMessage(resultSet.getString("message"));
            messageList.add(message);
        }
        //关闭数据库
        statement.close();
        connection.close();
        return messageList;
    }

    //往数据库存放数据
    private void save(Message message) throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造sql
        String sql = "insert into message values(?, ?, ?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        //执行sql
        statement.setString(1, message.getFrom());
        statement.setString(2, message.getTo());
        statement.setString(3, message.getMessage());
        //打印日志
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);
        //关闭数据库连接
        statement.close();
        connection.close();
    }
}





